package com.ourteam.product.api;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ourteam.frame.web.OperationResult;
import com.ourteam.product.ui.dao.ProductUIActionDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIActionGroupDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIFormDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIFormFieldDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIFormFieldTypeDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIListDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIListOptionDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITabDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITabItemDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITableColumnDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITableColumnIconDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITableDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITreeDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITreeNodeBuilderDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITreeNodeDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIActionBean;
import com.ourteam.product.ui.domain.ProductUIActionGroupBean;
import com.ourteam.product.ui.domain.ProductUIActionGroupRelateBean;
import com.ourteam.product.ui.domain.ProductUIFormBean;
import com.ourteam.product.ui.domain.ProductUIFormFieldBean;
import com.ourteam.product.ui.domain.ProductUIFormFieldTypeBean;
import com.ourteam.product.ui.domain.ProductUIListBean;
import com.ourteam.product.ui.domain.ProductUIListOptionBean;
import com.ourteam.product.ui.domain.ProductUITabBean;
import com.ourteam.product.ui.domain.ProductUITabItemBean;
import com.ourteam.product.ui.domain.ProductUITableBean;
import com.ourteam.product.ui.domain.ProductUITableColumnBean;
import com.ourteam.product.ui.domain.ProductUITableColumnIconBean;
import com.ourteam.product.ui.domain.ProductUITreeBean;
import com.ourteam.product.ui.domain.ProductUITreeNodeBean;
import com.ourteam.product.ui.domain.ProductUITreeNodeBuilderBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.model.action.ActionBean;
import net.ui.model.action.ActionGroupBean;
import net.ui.model.action.ActionSetBean;
import net.ui.model.action.ActionSetConfigBean;
import net.ui.model.constlist.ListData;
import net.ui.model.constlist.ListDataContainer;
import net.ui.model.constlist.ListDataFactory;
import net.ui.model.form.FormBean;
import net.ui.model.form.FormPropertyBean;
import net.ui.model.form.FormSetConfig;
import net.ui.model.tab.TabConfig;
import net.ui.model.tab.TabbedConfig;
import net.ui.model.tab.TabbedPaneConfig;
import net.ui.model.table.ColumnBean;
import net.ui.model.table.ColumnImageBean;
import net.ui.model.table.TableBean;
import net.ui.model.table.TableConfig;
import net.ui.model.tree.NodeBean;
import net.ui.model.tree.SubNodeBuilderBean;
import net.ui.model.tree.TreeBean;
import net.ui.model.tree.TreeConfigAgent;
import net.ui.model.tree.TreeNodeBean;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/UI/import")
public class ImportUIDataFileAPI {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@RequestMapping("/importUIActionFile/{uiDomainId}")
	@ResponseBody
	public OperationResult importUIActionFile(@PathVariable long uiDomainId,
			@RequestParam(value = "dataFiles[]") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		for (MultipartFile fileItem : files) {

			if (fileItem.isEmpty()) {
				continue;
			}

			ActionSetConfigBean configBean = new ActionSetConfigBean();

			configBean.init(fileItem.getInputStream());

			ActionSetBean actionSetBean = configBean.getActionSet();

			ActionBean[] actionBeans = actionSetBean.getActions();

			ActionGroupBean[] actionGroupBeans = actionSetBean.getActionGroups();

			Map<String, Long> actionBeanIdMap = new HashMap<>();

			for (int i = 0; i < actionBeans.length; i++) {

				ProductUIActionBean actionBean = new ProductUIActionBean();

				actionBean.setCode(actionBeans[i].getId());

				actionBean.setConfirmInfo(actionBeans[i].getConfirmInfoKey());

				actionBean.setDisableScript(actionBeans[i].getDisabled());

				actionBean.setDomainId(uiDomainId);

				actionBean.setHandler(actionBeans[i].getHandler());

				actionBean.setIcon(actionBeans[i].getIcon());

				actionBean.setLabelText(actionBeans[i].getLabel());

				actionBean.setLabelKey(actionBeans[i].getLabelKey());

				actionBean.setRemarks(actionBeans[i].getDesc());

				actionBean.setStyle(actionBeans[i].getStyle());

				actionBean.setType(actionBeans[i].getType());

				actionBean.setVisibleScript(actionBeans[i].getVisible());

				saveAction(actionBean);

				actionBeanIdMap.put(actionBean.getCode(), actionBean.getId());

				importSubActions(actionBean, actionBeans[i].getSubActionBeans());
			}

			for (int i = 0; i < actionGroupBeans.length; i++) {

				ProductUIActionGroupBean actionGroupBean = new ProductUIActionGroupBean();

				actionGroupBean.setActionGroupCode(actionGroupBeans[i].getId());

				actionGroupBean.setDomainId(uiDomainId);

				actionGroupBean.setRemarks(actionGroupBeans[i].getDesc());

				saveActionGroup(actionGroupBean);

				ActionBean[] relatedActionBeans = actionGroupBeans[i].getActionBeans();

				for (int j = 0; j < relatedActionBeans.length; j++) {

					ProductUIActionGroupRelateBean relateBean = new ProductUIActionGroupRelateBean();

					relateBean.setActionGroupId(actionGroupBean.getId());

					if (actionBeanIdMap.containsKey(relatedActionBeans[j].getId()) == false) {
						actionBeanIdMap.put(relatedActionBeans[j].getId(),
								getActionIdByCode(uiDomainId, relatedActionBeans[j].getId()));
					}

					relateBean.setRelatedActionId(actionBeanIdMap.get(relatedActionBeans[j].getId()));

					relateBean.setConfirmInfo(relatedActionBeans[j].getConfirmInfoKey());

					relateBean.setDisableScript(relatedActionBeans[j].getDisabled());

					relateBean.setHandler(relatedActionBeans[j].getHandler());

					relateBean.setIcon(relatedActionBeans[j].getIcon());

					relateBean.setLabelText(relatedActionBeans[j].getLabel());

					relateBean.setLabelKey(relatedActionBeans[j].getLabelKey());

					relateBean.setRemarks(relatedActionBeans[j].getDesc());

					relateBean.setStyle(relatedActionBeans[j].getStyle());

					relateBean.setVisibleScript(relatedActionBeans[j].getVisible());

					PRODUCT_UI_SERVICE.addNewProductUIActionGroupRelate(relateBean);
				}
			}

		}

		return OperationResult.SUCCESS_RESULT;
	}

	protected long getActionIdByCode(long domainId, String code) throws Exception {

		ProductUIActionDAOQueryBean queryBean = new ProductUIActionDAOQueryBean();

		queryBean.createCriteria().andCodeEqualTo(code);

		ProductUIActionBean[] actionBeans = PRODUCT_UI_SERVICE.queryProductUIActions(queryBean);

		ProductUIActionBean actionBean = null;

		ProductUIActionBean commonActionBean = null;

		for (int i = 0; i < actionBeans.length; i++) {
			if (actionBeans[i].getDomainId() == domainId) {
				actionBean = actionBeans[i];
				break;
			}
			if ("Common".equals(actionBeans[i].getDomainCode())) {
				commonActionBean = actionBeans[i];
			}
		}

		if (actionBean == null && commonActionBean != null) {
			actionBean = commonActionBean;
		}

		return actionBean != null ? actionBean.getId() : 0;
	}

	protected void importSubActions(ProductUIActionBean parentAction, ActionBean[] subActions) throws Exception {

		for (int i = 0; i < subActions.length; i++) {

			ProductUIActionBean subActionBean = new ProductUIActionBean();

			subActionBean.setCode(subActions[i].getId());

			subActionBean.setConfirmInfo(subActions[i].getConfirmInfoKey());

			subActionBean.setDisableScript(subActions[i].getDisabled());

			subActionBean.setDomainId(parentAction.getDomainId());

			subActionBean.setParentId(parentAction.getId());

			subActionBean.setHandler(subActions[i].getHandler());

			subActionBean.setIcon(subActions[i].getIcon());

			subActionBean.setLabelText(subActions[i].getLabel());

			subActionBean.setLabelKey(subActions[i].getLabelKey());

			subActionBean.setRemarks(subActions[i].getDesc());

			subActionBean.setStyle(subActions[i].getStyle());

			subActionBean.setType(subActions[i].getType());

			subActionBean.setVisibleScript(subActions[i].getVisible());

			saveAction(subActionBean);

			importSubActions(subActionBean, subActions[i].getSubActionBeans());

		}
	}

	protected void saveActionGroup(ProductUIActionGroupBean actionGroupBean) throws Exception {

		ProductUIActionGroupDAOQueryBean queryBean = new ProductUIActionGroupDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(actionGroupBean.getDomainId())
				.andActionGroupCodeEqualTo(actionGroupBean.getActionGroupCode());

		ProductUIActionGroupBean[] actionGroupBeans = PRODUCT_UI_SERVICE.queryProductUIActionGroups(queryBean);

		if (ArrayUtils.isEmpty(actionGroupBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUIActionGroup(actionGroupBean);

		} else {
			for (int i = 0; i < actionGroupBeans.length; i++) {
				ProductUIActionGroupBean productUIActionGroupBean = actionGroupBeans[i];

				actionGroupBean.setId(productUIActionGroupBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUIActionGroup(actionGroupBean);
			}
		}

	}

	protected void saveAction(ProductUIActionBean actionBean) throws Exception {

		ProductUIActionDAOQueryBean queryBean = new ProductUIActionDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(actionBean.getDomainId())
				.andParentIdEqualTo(actionBean.getParentId()).andCodeEqualTo(actionBean.getCode());

		ProductUIActionBean[] actionBeans = PRODUCT_UI_SERVICE.queryProductUIActions(queryBean);

		if (ArrayUtils.isEmpty(actionBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUIAction(actionBean);

		} else {
			for (int i = 0; i < actionBeans.length; i++) {
				ProductUIActionBean productUIActionBean = actionBeans[i];

				actionBean.setId(productUIActionBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUIAction(actionBean);
			}
		}

	}

	@RequestMapping("/importUIListDataFile/{uiDomainId}")
	@ResponseBody
	public OperationResult importUIListDataFile(@PathVariable long uiDomainId,
			@RequestParam(value = "dataFiles[]") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		for (MultipartFile fileItem : files) {

			if (fileItem.isEmpty()) {
				continue;
			}

			ListDataFactory listDataFactory = new ListDataFactory();

			listDataFactory.initListData(fileItem.getInputStream());

			ListDataContainer[] listDataContainers = listDataFactory.getListDataContainers();

			for (int i = 0; i < listDataContainers.length; i++) {
				ListDataContainer listDataContainer = listDataContainers[i];

				ProductUIListBean listBean = new ProductUIListBean();

				listBean.setCode(listDataContainer.getName());

				listBean.setDataProperty(listDataContainer.getDataProperty());

				listBean.setDomainId(uiDomainId);

				listBean.setIconProperty(listDataContainer.getIconProperty());

				listBean.setLabelProperty(listDataContainer.getLabelProperty());

				listBean.setRemarks(listDataContainer.getDesc());

				listBean.setType(listDataContainer.getType());

				saveList(listBean);

				Collection<ListData> ListOptions = listDataContainer.getListData();

				for (Iterator<ListData> iterator = ListOptions.iterator(); iterator.hasNext();) {
					ListData listData = iterator.next();

					ProductUIListOptionBean optionBean = new ProductUIListOptionBean();

					optionBean.setDataIcon(listData.getIcon());

					optionBean.setDataLabel(listData.getLabel());

					optionBean.setDataValue(String.valueOf(listData.getData()));

					optionBean.setListId(listBean.getId());

					saveListOption(optionBean);
				}

			}

		}

		return OperationResult.SUCCESS_RESULT;
	}

	protected void saveList(ProductUIListBean listBean) throws Exception {

		ProductUIListDAOQueryBean queryBean = new ProductUIListDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(listBean.getDomainId()).andCodeEqualTo(listBean.getCode());

		ProductUIListBean[] listBeans = PRODUCT_UI_SERVICE.queryProductUILists(queryBean);

		if (ArrayUtils.isEmpty(listBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUIList(listBean);

		} else {
			for (int i = 0; i < listBeans.length; i++) {
				ProductUIListBean uiListBean = listBeans[i];

				listBean.setId(uiListBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUIList(listBean);
			}
		}

	}

	protected void saveListOption(ProductUIListOptionBean listOptioBean) throws Exception {

		ProductUIListOptionDAOQueryBean queryBean = new ProductUIListOptionDAOQueryBean();

		ProductUIListOptionDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		criteria.andListIdEqualTo(listOptioBean.getListId());

		if (StringUtils.isEmpty(listOptioBean.getDataValue())) {
			criteria.andDataValueIsEmpty();
		} else {
			criteria.andDataValueEqualTo(listOptioBean.getDataValue());
		}

		ProductUIListOptionBean[] listOptionBeans = PRODUCT_UI_SERVICE.queryProductUIListOptions(queryBean);

		if (ArrayUtils.isEmpty(listOptionBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUIListOption(listOptioBean);

		} else {
			for (int i = 0; i < listOptionBeans.length; i++) {
				ProductUIListOptionBean uiListOptionBean = listOptionBeans[i];

				listOptioBean.setId(uiListOptionBean.getId());
				listOptioBean.setSortIndex(uiListOptionBean.getSortIndex());

				PRODUCT_UI_SERVICE.modifyProductUIListOption(listOptioBean);
			}
		}

	}

	@RequestMapping("/importUIFormFile/{uiDomainId}")
	@ResponseBody
	public OperationResult importUIFormFile(@PathVariable long uiDomainId,
			@RequestParam(value = "dataFiles[]") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		for (MultipartFile fileItem : files) {

			if (fileItem.isEmpty()) {
				continue;
			}

			FormSetConfig formSetConfig = FormSetConfig.init(fileItem.getInputStream());

			Collection<FormBean> forms = formSetConfig.getFormList();

			for (Iterator<FormBean> iterator = forms.iterator(); iterator.hasNext();) {
				FormBean formBean = iterator.next();

				ProductUIFormBean uiFormBean = new ProductUIFormBean();

				uiFormBean.setCode(formBean.getId());

				uiFormBean.setDomainId(uiDomainId);

				uiFormBean.setNumColumns(formBean.getFormColumns());

				uiFormBean.setRemarks(formBean.getDesc());

				uiFormBean.setTitle(formBean.getTitle());

				uiFormBean.setTitleKey(formBean.getTitleKey());

				saveForm(uiFormBean);

				Collection<FormPropertyBean> formProperties = formBean.getFormProperties();

				for (Iterator<FormPropertyBean> iterator2 = formProperties.iterator(); iterator2.hasNext();) {
					FormPropertyBean formPropertyBean = iterator2.next();

					ProductUIFormFieldBean formFieldBean = new ProductUIFormFieldBean();

					formFieldBean.setFormId(uiFormBean.getId());

					formFieldBean
							.setAllowEmpty(BooleanUtils.toString(formPropertyBean.getAllowNull(), "true", "false"));

					formFieldBean.setCode(formPropertyBean.getId());

					if (StringUtils.isNoneEmpty(formPropertyBean.getDataSrc())) {
						formFieldBean.setDataSource(
								String.valueOf(getDataSourceIdByCode(uiDomainId, formPropertyBean.getDataSrc())));
					}

					formFieldBean.setFieldColumn(formPropertyBean.getColumn());

					formFieldBean.setFieldSpan(formPropertyBean.getSpan());

					formFieldBean.setReadOnly(BooleanUtils.toString(formPropertyBean.getReadOnly(), "true", "false"));

					formFieldBean.setRemarks(formPropertyBean.getDesc());

					formFieldBean.setRemenberValue(
							BooleanUtils.toString(formPropertyBean.getRemenberValue(), "true", "false"));

					formFieldBean.setStyle(formPropertyBean.getStyleDesc());

					formFieldBean.setTipInfo(formPropertyBean.getTipInfo());

					formFieldBean.setTitle(formPropertyBean.getTitle());

					formFieldBean.setTitleKey(formPropertyBean.getTitleKey());

					formFieldBean.setTypeId(getFormTypeId(formPropertyBean.getType()));

					saveFormField(formFieldBean);

				}

			}

		}

		return OperationResult.SUCCESS_RESULT;
	}

	protected long getFormTypeId(String formFieldTypeCode) throws Exception {

		ProductUIFormFieldTypeDAOQueryBean queryBean = new ProductUIFormFieldTypeDAOQueryBean();

		queryBean.createCriteria().andTypeWidgetEqualTo(formFieldTypeCode);

		ProductUIFormFieldTypeBean[] fieldTypeBeans = PRODUCT_UI_SERVICE.queryProductUIFormFieldTypes(queryBean);

		if (ArrayUtils.isNotEmpty(fieldTypeBeans)) {
			return fieldTypeBeans[0].getId();
		} else {

			ProductUIFormFieldTypeBean typeBean = new ProductUIFormFieldTypeBean();

			typeBean.setTypeWidget(formFieldTypeCode);

			typeBean.setTypeName(formFieldTypeCode);

			PRODUCT_UI_SERVICE.addNewProductUIFormFieldType(typeBean);

			return typeBean.getId();

		}

	}

	protected long getDataSourceIdByCode(long domainId, String code) throws Exception {

		ProductUIListDAOQueryBean queryBean = new ProductUIListDAOQueryBean();

		queryBean.createCriteria().andCodeEqualTo(code);

		ProductUIListBean[] listBeans = PRODUCT_UI_SERVICE.queryProductUILists(queryBean);

		ProductUIListBean listBean = null;

		ProductUIListBean commonListBean = null;

		for (int i = 0; i < listBeans.length; i++) {
			if (listBeans[i].getDomainId() == domainId) {
				listBean = listBeans[i];
				break;
			}
			if ("Common".equals(listBeans[i].getDomainCode())) {
				commonListBean = listBeans[i];
			}
		}

		if (listBean == null && commonListBean != null) {
			listBean = commonListBean;
		}

		return listBean != null ? listBean.getId() : 0;
	}

	protected void saveForm(ProductUIFormBean formBean) throws Exception {

		ProductUIFormDAOQueryBean queryBean = new ProductUIFormDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(formBean.getDomainId()).andCodeEqualTo(formBean.getCode());

		ProductUIFormBean[] listBeans = PRODUCT_UI_SERVICE.queryProductUIForms(queryBean);

		if (ArrayUtils.isEmpty(listBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUIForm(formBean);

		} else {
			for (int i = 0; i < listBeans.length; i++) {
				ProductUIFormBean UIFormBean = listBeans[i];

				formBean.setId(UIFormBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUIForm(formBean);
			}
		}

	}

	protected void saveFormField(ProductUIFormFieldBean formFieldBean) throws Exception {

		ProductUIFormFieldDAOQueryBean queryBean = new ProductUIFormFieldDAOQueryBean();

		queryBean.createCriteria().andFormIdEqualTo(formFieldBean.getFormId()).andCodeEqualTo(formFieldBean.getCode());

		ProductUIFormFieldBean[] fieldBeans = PRODUCT_UI_SERVICE.queryProductUIFormFields(queryBean);

		if (ArrayUtils.isEmpty(fieldBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUIFormField(formFieldBean);

		} else {
			for (int i = 0; i < fieldBeans.length; i++) {
				ProductUIFormFieldBean UIFormFieldBean = fieldBeans[i];

				formFieldBean.setId(UIFormFieldBean.getId());
				formFieldBean.setSortIndex(UIFormFieldBean.getSortIndex());

				PRODUCT_UI_SERVICE.modifyProductUIFormField(formFieldBean);
			}
		}

	}

	@RequestMapping("/importUITableFile/{uiDomainId}")
	@ResponseBody
	public OperationResult importUITableFile(@PathVariable long uiDomainId,
			@RequestParam(value = "dataFiles[]") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		for (MultipartFile fileItem : files) {

			if (fileItem.isEmpty()) {
				continue;
			}

			TableConfig tableConfig = TableConfig.init(fileItem.getInputStream());

			Collection<TableBean> tableBeans = tableConfig.getTableList();

			for (Iterator<TableBean> iterator = tableBeans.iterator(); iterator.hasNext();) {
				TableBean tableBean = iterator.next();

				ProductUITableBean uiTableBean = new ProductUITableBean();

				uiTableBean.setDomainId(uiDomainId);

				uiTableBean.setCode(tableBean.getId());

				uiTableBean.setRemarks(tableBean.getDesc());

				uiTableBean.setRowId(tableBean.getRowId());

				saveTable(uiTableBean);

				Collection<ColumnBean> columnBeans = tableBean.getColumnList();

				for (Iterator<ColumnBean> iterator2 = columnBeans.iterator(); iterator2.hasNext();) {
					ColumnBean columnBean = iterator2.next();

					ProductUITableColumnBean uiTableColumnBean = new ProductUITableColumnBean();

					uiTableColumnBean.setTableId(uiTableBean.getId());

					uiTableColumnBean.setAlign(columnBean.getAlign());

					uiTableColumnBean.setCanFilter(BooleanUtils.toString(columnBean.isCanFilter(), "true", "false"));

					uiTableColumnBean.setCanReOrder(BooleanUtils.toString(columnBean.isCanReOrder(), "true", "false"));

					uiTableColumnBean.setCanResize(BooleanUtils.toString(columnBean.isCanResize(), "true", "false"));

					uiTableColumnBean.setCanSort(BooleanUtils.toString(columnBean.isCanSort(), "true", "false"));

					uiTableColumnBean.setFrozen(BooleanUtils.toString(columnBean.isFrozen(), "true", "false"));

					uiTableColumnBean.setCode(columnBean.getProperty());

					uiTableColumnBean.setDataType(columnBean.getDataType());

					uiTableColumnBean.setListDataSource(
							String.valueOf(getDataSourceIdByCode(uiDomainId, columnBean.getListDataSource())));

					uiTableColumnBean.setShowValue(BooleanUtils.toString(columnBean.getShowValue(), "true", "false"));

					uiTableColumnBean.setTitle(columnBean.getLabel());

					uiTableColumnBean.setTitleKey(columnBean.getLabelKey());

					uiTableColumnBean.setTitleIcon(columnBean.getIcon());

					uiTableColumnBean.setWidth(columnBean.getWidth());

					saveTableColumn(uiTableColumnBean);

					Collection<ColumnImageBean> columnImageBeans = columnBean.getImageList();

					for (Iterator<ColumnImageBean> iterator3 = columnImageBeans.iterator(); iterator3.hasNext();) {
						ColumnImageBean columnImageBean = iterator3.next();

						ProductUITableColumnIconBean iconBean = new ProductUITableColumnIconBean();

						iconBean.setColumnId(uiTableColumnBean.getId());

						iconBean.setDataValue(columnImageBean.getPattern());

						iconBean.setDataIcon(columnImageBean.getImage());

						saveTableColumnIcon(iconBean);
					}

				}

			}

		}

		return OperationResult.SUCCESS_RESULT;
	}

	protected void saveTable(ProductUITableBean tableBean) throws Exception {

		ProductUITableDAOQueryBean queryBean = new ProductUITableDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(tableBean.getDomainId()).andCodeEqualTo(tableBean.getCode());

		ProductUITableBean[] tableBeans = PRODUCT_UI_SERVICE.queryProductUITables(queryBean);

		if (ArrayUtils.isEmpty(tableBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITable(tableBean);

		} else {
			for (int i = 0; i < tableBeans.length; i++) {

				ProductUITableBean uiTableBean = tableBeans[i];

				tableBean.setId(uiTableBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUITable(tableBean);
			}
		}

	}

	protected void saveTableColumn(ProductUITableColumnBean tableFieldBean) throws Exception {

		ProductUITableColumnDAOQueryBean queryBean = new ProductUITableColumnDAOQueryBean();

		queryBean.createCriteria().andTableIdEqualTo(tableFieldBean.getTableId())
				.andCodeEqualTo(tableFieldBean.getCode());

		ProductUITableColumnBean[] fieldBeans = PRODUCT_UI_SERVICE.queryProductUITableColumns(queryBean);

		if (ArrayUtils.isEmpty(fieldBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITableColumn(tableFieldBean);

		} else {
			for (int i = 0; i < fieldBeans.length; i++) {
				ProductUITableColumnBean columnBean = fieldBeans[i];

				tableFieldBean.setId(columnBean.getId());
				tableFieldBean.setSortIndex(columnBean.getSortIndex());

				PRODUCT_UI_SERVICE.modifyProductUITableColumn(tableFieldBean);
			}
		}

	}

	protected void saveTableColumnIcon(ProductUITableColumnIconBean tableFieldIconBean) throws Exception {

		ProductUITableColumnIconDAOQueryBean queryBean = new ProductUITableColumnIconDAOQueryBean();

		queryBean.createCriteria().andColumnIdEqualTo(tableFieldIconBean.getColumnId())
				.andDataValueEqualTo(tableFieldIconBean.getDataValue());

		ProductUITableColumnIconBean[] fieldIconBeans = PRODUCT_UI_SERVICE.queryProductUITableColumnIcons(queryBean);

		if (ArrayUtils.isEmpty(fieldIconBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITableColumnIcon(tableFieldIconBean);

		} else {
			for (int i = 0; i < fieldIconBeans.length; i++) {
				ProductUITableColumnIconBean columnIconBean = fieldIconBeans[i];

				tableFieldIconBean.setId(columnIconBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUITableColumnIcon(tableFieldIconBean);
			}
		}

	}
	
	@RequestMapping("/importUITreeFile/{uiDomainId}")
	@ResponseBody
	public OperationResult importUITreeFile(@PathVariable long uiDomainId,
			@RequestParam(value = "dataFiles[]") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		for (MultipartFile fileItem : files) {

			if (fileItem.isEmpty()) {
				continue;
			}
			
			TreeBean treeBean = TreeConfigAgent.getTreeConfig(fileItem.getInputStream());
			
			ProductUITreeBean uiTreeBean = new ProductUITreeBean();
			
			uiTreeBean.setCode(fileItem.getOriginalFilename());
			
			uiTreeBean.setDomainId(uiDomainId);
			
			saveTree(uiTreeBean);
			
			List<NodeBean> nodeBeans = treeBean.getNodeIdIndex();
			
			List<TreeNodeBean> rootNodeBeans = treeBean.getRootNodeList();
			
			List<String> rootNodeIds = new ArrayList<>();
			
			for (Iterator<TreeNodeBean> iterator = rootNodeBeans.iterator(); iterator.hasNext();) {
				TreeNodeBean treeNodeBean = iterator.next();
				
				rootNodeIds.add(treeNodeBean.getId());
				
				getTreeRootNodeIds(treeNodeBean,rootNodeIds);
				
			}
			
			for (Iterator<NodeBean> iterator = nodeBeans.iterator(); iterator.hasNext();) {
				NodeBean nodeBean = iterator.next();
				
				ProductUITreeNodeBean treeNodeBean = new ProductUITreeNodeBean();
				
				treeNodeBean.setTreeId(uiTreeBean.getId());
				
				treeNodeBean.setCode(nodeBean.getId());
				
				treeNodeBean.setIcon(nodeBean.getImage());
				
				treeNodeBean.setIsLeaf(BooleanUtils.toString(nodeBean.getLeaf(), "true", "false") );
				
				treeNodeBean.setIsRoot(BooleanUtils.toString(rootNodeIds.contains(nodeBean.getId()), "true", "false"));
				
				treeNodeBean.setRemarks(nodeBean.getDesc());
				
				treeNodeBean.setTitle(nodeBean.getLabel());
				
				treeNodeBean.setTitleKey(nodeBean.getLabelKey());
				
				saveTreeNode(treeNodeBean);
				
				List<SubNodeBuilderBean> nodeBuilders = nodeBean.getSubNodBuilderList();
				
				for (Iterator<SubNodeBuilderBean> iterator2 = nodeBuilders.iterator(); iterator2.hasNext();) {
					SubNodeBuilderBean subNodeBuilderBean = iterator2.next();
					
					ProductUITreeNodeBuilderBean treeNodeBuilderBean = new ProductUITreeNodeBuilderBean();
					
					treeNodeBuilderBean.setNodeId(treeNodeBean.getId());
					
					treeNodeBuilderBean.setCode(subNodeBuilderBean.getId());
					
					treeNodeBuilderBean.setNodeBuilder(subNodeBuilderBean.getClassName());
					
					saveTreeNodeBuilder(treeNodeBuilderBean);
					
				}
			}
			
		}
		
		return OperationResult.SUCCESS_RESULT;
	}
	
	protected void getTreeRootNodeIds(TreeNodeBean rootNode,List<String> rootIds) {
		
		List<TreeNodeBean> subRootNode =  rootNode.getSubNodeList();
		
		for (Iterator<TreeNodeBean> iterator = subRootNode.iterator(); iterator.hasNext();) {
			TreeNodeBean treeNodeBean = iterator.next();
			
			rootIds.add(treeNodeBean.getId());
			
			getTreeRootNodeIds(treeNodeBean,rootIds);
			
		}
	}
	
	protected void saveTree(ProductUITreeBean treeBean) throws Exception {

		ProductUITreeDAOQueryBean queryBean = new ProductUITreeDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(treeBean.getDomainId()).andCodeEqualTo(treeBean.getCode());

		ProductUITreeBean[] tableBeans = PRODUCT_UI_SERVICE.queryProductUITrees(queryBean);

		if (ArrayUtils.isEmpty(tableBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITree(treeBean);

		} else {
			for (int i = 0; i < tableBeans.length; i++) {

				ProductUITreeBean UITreeBean = tableBeans[i];

				treeBean.setId(UITreeBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUITree(treeBean);
			}
		}

	}
	
	protected void saveTreeNode(ProductUITreeNodeBean treeNodeBean) throws Exception {

		ProductUITreeNodeDAOQueryBean queryBean = new ProductUITreeNodeDAOQueryBean();

		queryBean.createCriteria().andTreeIdEqualTo(treeNodeBean.getTreeId()).andCodeEqualTo(treeNodeBean.getCode());

		ProductUITreeNodeBean[] tableNodeBeans = PRODUCT_UI_SERVICE.queryProductUITreeNodes(queryBean);

		if (ArrayUtils.isEmpty(tableNodeBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITreeNode(treeNodeBean);

		} else {
			for (int i = 0; i < tableNodeBeans.length; i++) {

				ProductUITreeNodeBean uiTreeNodeBean = tableNodeBeans[i];

				treeNodeBean.setId(uiTreeNodeBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUITreeNode(treeNodeBean);
			}
		}

	}
	
	protected void saveTreeNodeBuilder(ProductUITreeNodeBuilderBean treeNodeBuilderBean) throws Exception {

		ProductUITreeNodeBuilderDAOQueryBean queryBean = new ProductUITreeNodeBuilderDAOQueryBean();

		queryBean.createCriteria().andNodeIdEqualTo(treeNodeBuilderBean.getNodeId()).andCodeEqualTo(treeNodeBuilderBean.getCode());

		ProductUITreeNodeBuilderBean[] tableNodeBuilderBeans = PRODUCT_UI_SERVICE.queryProductUITreeNodeBuilders(queryBean);

		if (ArrayUtils.isEmpty(tableNodeBuilderBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITreeNodeBuilder(treeNodeBuilderBean);

		} else {
			for (int i = 0; i < tableNodeBuilderBeans.length; i++) {

				ProductUITreeNodeBuilderBean uiTreeNodeBuilderBean = tableNodeBuilderBeans[i];

				treeNodeBuilderBean.setId(uiTreeNodeBuilderBean.getId());
				
				treeNodeBuilderBean.setSortIndex(uiTreeNodeBuilderBean.getSortIndex());

				PRODUCT_UI_SERVICE.modifyProductUITreeNodeBuilder(treeNodeBuilderBean);
			}
		}

	}
	
	@RequestMapping("/importUITabFile/{uiDomainId}")
	@ResponseBody
	public OperationResult importUITabFile(@PathVariable long uiDomainId,
			@RequestParam(value = "dataFiles[]") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		for (MultipartFile fileItem : files) {

			if (fileItem.isEmpty()) {
				continue;
			}
			
			TabbedConfig tabbedConfig =TabbedConfig.parse(fileItem.getInputStream());
			
			List<TabbedPaneConfig> paneConfigs = tabbedConfig.getTabbedPaneList();
			
			for (Iterator<TabbedPaneConfig> iterator = paneConfigs.iterator(); iterator.hasNext();) {
				TabbedPaneConfig tabbedPaneConfig = iterator.next();
				
				ProductUITabBean tabBean = new ProductUITabBean();
				
				tabBean.setCode(tabbedPaneConfig.getId());
				
				tabBean.setDomainId(uiDomainId);
				
				tabBean.setRemarks(tabbedPaneConfig.getDesc());
				
				saveTab(tabBean);
				
				List<TabConfig> tabConfigs = tabbedPaneConfig.getTabPageList();
				
				for (Iterator<TabConfig> iterator2 = tabConfigs.iterator(); iterator2.hasNext();) {
					TabConfig tabConfig = iterator2.next();
					
					ProductUITabItemBean tabItemBean = new ProductUITabItemBean();
					
					tabItemBean.setTabId(tabBean.getId());
					
					tabItemBean.setCode(tabConfig.getName());
					
					tabItemBean.setCompentClass(tabConfig.getCompentclass());
					
					tabItemBean.setIcon(tabConfig.getIcon());
					
					tabItemBean.setTitle(tabConfig.getLabel());
					
					tabItemBean.setTitleKey(tabConfig.getLabelKey());
					
					saveTabItem(tabItemBean);
				}
			}
			
			
		}
		
		return OperationResult.SUCCESS_RESULT;
	}
	
	protected void saveTab(ProductUITabBean tabBean) throws Exception {

		ProductUITabDAOQueryBean queryBean = new ProductUITabDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(tabBean.getDomainId()).andCodeEqualTo(tabBean.getCode());

		ProductUITabBean[] tabBeans = PRODUCT_UI_SERVICE.queryProductUITabs(queryBean);

		if (ArrayUtils.isEmpty(tabBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITab(tabBean);

		} else {
			for (int i = 0; i < tabBeans.length; i++) {

				ProductUITabBean uiTabBean = tabBeans[i];

				tabBean.setId(uiTabBean.getId());

				PRODUCT_UI_SERVICE.modifyProductUITab(tabBean);
			}
		}

	}
	
	protected void saveTabItem(ProductUITabItemBean tabItemBean) throws Exception {

		ProductUITabItemDAOQueryBean queryBean = new ProductUITabItemDAOQueryBean();

		queryBean.createCriteria().andTabIdEqualTo(tabItemBean.getTabId()).andCodeEqualTo(tabItemBean.getCode());

		ProductUITabItemBean[] tabItemBeans = PRODUCT_UI_SERVICE.queryProductUITabItems(queryBean);

		if (ArrayUtils.isEmpty(tabItemBeans)) {

			PRODUCT_UI_SERVICE.addNewProductUITabItem(tabItemBean);

		} else {
			for (int i = 0; i < tabItemBeans.length; i++) {

				ProductUITabItemBean uiTabItemBean = tabItemBeans[i];

				tabItemBean.setId(uiTabItemBean.getId());
				
				tabItemBean.setSortIndex(uiTabItemBean.getSortIndex());

				PRODUCT_UI_SERVICE.modifyProductUITabItem(tabItemBean);
			}
		}

	}

}
